# -*- coding: utf8 -*-
# humanlike.py
"""
Function attributes:
* load: boolean. Optional. If False the function will not load.
* name: string. Optional.
* priority: string. Optional. Three values are available: "high", "medium" or "low",
  default value: "medium".
* thread: boolean. Optional. If True, function will work by starting a thread.
* rule: string or regexp object. Required.
* aliases: list of string or regexp objects. Optional.
* fullmsg: boolean. Optional. If True, pattern is searched on whole message, else
  pattern is a command, the message must begin with a sign.

* access
* evttypes: must be tuple
* skip_sentinel: skip sentinel instance
* limit
* channels
* url
"""
import re
from core.ircfuncs import irc_lower, is_channel

def type_(bot, evt, cmd):
	#bot.write(evt, cmd.arguments.replace(r"\-", "-"))
	bot.write(evt, cmd.cmdline.replace(r"\-", "-"))
type_.rule = "type"
type_.level= "support"

def send_to_(bot, evt, cmd):
	".sendto [:a|:n] [#channel|@nick] <message> &B;-&N; send a &+g;<message>&N; to a &+g;#channel&N; or &+g;@nick&N;. &+g;:a&N; sends an action, &+g;:n&N; sends a notice."
	msg_type=""; receptor=""
	tokens = evt.msg.split(" ")[1:]
	for token in tokens[:2]:
		#if token.startswith(":") and token[1] in "an":
		if re.search(":[an]", token):
			msg_type = token
			tokens.remove(token)
		elif token.startswith("@"):
			receptor = token
			tokens.remove(token)
		elif token.startswith("#"):
			receptor = token
			tokens.remove(token)
	msg = " ".join(tokens)

	if msg_type:
		if msg_type == ":a": msg_type = bot.action
		elif msg_type == ":n": msg_type = bot.notice
	else:
		msg_type = bot.privmsg
	if not receptor:
		receptor = def_chan or evt.target

	if is_channel(receptor) and not bot.channels.has_key(receptor):
		bot.reply(evt, "No such channel. I'm not in %s" % receptor)
		return True
	elif receptor.startswith("@") and not irc_lower(receptor[1:]) in [irc_lower(nick) for nick in bot.nicks.keys()]:
		bot.reply(evt, "unknown nick")
		return True
	if receptor.startswith("@"): receptor=receptor[1:]
	print "\nt: %r | r %r | m %r\n" % (msg_type, receptor, msg)
	bot.write_to(evt, msg_type, receptor, msg)
send_to_.rule = [":","sendto"]
send_to_.limit = 15
send_to_.level= "support"

def_chan = ""
def default_chan_(bot, evt, cmd):
	global def_chan
	if not cmd.arguments:
		bot.reply(evt, def_chan)
	else:
		def_chan = cmd.arguments
default_chan_.rule = "defchan"
default_chan_.level= "support"
